package com.lipuwater.agent.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseFragment;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.home.HomeDataBean;
import com.lipuwater.agent.http.bean.home.InstallRankingBean;
import com.lipuwater.agent.http.bean.home.SalesRankingBean;
import com.lipuwater.agent.http.bean.user.RoleConfig;
import com.lipuwater.agent.ui.activity.AccountActivity;
import com.lipuwater.agent.ui.activity.InstallRankingActivity;
import com.lipuwater.agent.ui.activity.MainActivity;
import com.lipuwater.agent.ui.activity.PersonnelListActivity;
import com.lipuwater.agent.ui.activity.ProductListActivity;
import com.lipuwater.agent.ui.activity.SalesRankingActivity;
import com.lipuwater.agent.ui.activity.after.AfterListActivity;
import com.lipuwater.agent.ui.activity.install.InstallListActivity;
import com.lipuwater.agent.ui.activity.sales.SalesListActivity;
import com.lipuwater.agent.ui.event.KeyDownEvent;
import com.lipuwater.agent.utils.MapToBeanUtil;
import com.lipuwater.agent.utils.onclick.NoDoubleClick;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;


/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/2/16.
 * Email:924686754@qq.com
 */
public class FragmentHome extends BaseFragment {
    @Bind(R.id.text_sales_count)
    TextView textSalesCount;
    @Bind(R.id.text_already_install)
    TextView textAlreadyInstall;
    @Bind(R.id.text_no_install)
    TextView textNoInstall;
    @Bind(R.id.text_sales_name1)
    TextView textSalesName1;
    @Bind(R.id.text_sales_count1)
    TextView textSalesCount1;
    @Bind(R.id.text_sales_name2)
    TextView textSalesName2;
    @Bind(R.id.text_sales_count2)
    TextView textSalesCount2;
    @Bind(R.id.text_sales_name3)
    TextView textSalesName3;
    @Bind(R.id.text_sales_count3)
    TextView textSalesCount3;
    @Bind(R.id.text_install_name1)
    TextView textInstallName1;
    @Bind(R.id.text_install_count1)
    TextView textInstallCount1;
    @Bind(R.id.text_install_name2)
    TextView textInstallName2;
    @Bind(R.id.text_install_count2)
    TextView textInstallCount2;
    @Bind(R.id.text_install_name3)
    TextView textInstallName3;
    @Bind(R.id.text_install_count3)
    TextView textInstallCount3;
    @Bind(R.id.layout_tab1)
    LinearLayout layoutTab1;
    @Bind(R.id.layout_tab2)
    LinearLayout layoutTab2;
    @Bind(R.id.layout_sales_service)
    TableLayout layoutSalesService;
    @Bind(R.id.layout_admin)
    TableLayout layoutAdmin;
    @Bind(R.id.image_tab1)
    ImageView imageTab1;
    @Bind(R.id.text_tab1)
    TextView textTab1;
    @Bind(R.id.image_tab2)
    ImageView imageTab2;
    @Bind(R.id.text_tab2)
    TextView textTab2;
    @Bind(R.id.layout_content)
    LinearLayout layoutContent;


    private View view;
    private NoDoubleClick mNoDoubleClick = new NoDoubleClick();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        mContentView.addView(view);
        if (roleType!=null&&roleType.equals(RoleConfig.SALES)) {
            //销售人员
            layoutAdmin.setVisibility(View.GONE);
            layoutSalesService.setVisibility(View.VISIBLE);
            imageTab1.setImageResource(R.drawable.img_order);
            textTab1.setText("快速开单");
            imageTab2.setImageResource(R.drawable.img_sales);
            textTab2.setText("销售单详情");
        } else if (roleType!=null&&roleType.equals(RoleConfig.SERVICE)) {
            //服务人员
            layoutAdmin.setVisibility(View.GONE);
            layoutSalesService.setVisibility(View.VISIBLE);
            imageTab1.setImageResource(R.drawable.img_setting);
            textTab1.setText("安装单管理");
            imageTab2.setImageResource(R.drawable.img_after);
            textTab2.setText("售后单管理");
        } else if (roleType!=null&&roleType.equals(RoleConfig.MANAGER)) {
            //管理员
            View tabView = layoutContent.getChildAt(0);
            View meterView = layoutContent.getChildAt(2);
            layoutContent.removeView(tabView);
            layoutContent.removeView(meterView);
            layoutContent.addView(meterView, 0);
            layoutContent.addView(tabView);
        }
        getHomeData();
    }

    @OnClick({R.id.layout_order, R.id.layout_sales, R.id.layout_install, R.id.layout_after, R.id.layout_personnel, R.id.layout_account, R.id.layout_sales_des, R.id.layout_install_des, R.id.layout_tab1, R.id.layout_tab2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_order:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), ProductListActivity.class));
                break;
            case R.id.layout_sales:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), SalesListActivity.class));
                break;
            case R.id.layout_install:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), InstallListActivity.class));
                break;
            case R.id.layout_after:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), AfterListActivity.class));
                break;
            case R.id.layout_personnel:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), PersonnelListActivity.class));
                break;
            case R.id.layout_account:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), AccountActivity.class));
                break;
            case R.id.layout_sales_des:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), SalesRankingActivity.class));
                break;
            case R.id.layout_install_des:
                if (mNoDoubleClick.check()) return;
                startActivity(new Intent(getActivity(), InstallRankingActivity.class));
                break;
            case R.id.layout_tab1:
                if (mNoDoubleClick.check()) return;
                if (roleType.equals(RoleConfig.SALES)) {
                    startActivity(new Intent(getActivity(), ProductListActivity.class));
                } else if (roleType.equals(RoleConfig.SERVICE)) {
                    startActivity(new Intent(getActivity(), InstallListActivity.class));
                }
                break;
            case R.id.layout_tab2:
                if (mNoDoubleClick.check()) return;
                if (roleType.equals(RoleConfig.SALES)) {
                    startActivity(new Intent(getActivity(), SalesListActivity.class));
                } else if (roleType.equals(RoleConfig.SERVICE)) {
                    startActivity(new Intent(getActivity(), AfterListActivity.class));
                }
                break;
        }
    }

    private void getHomeData() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        ApiRequestsBasis.getHomeData(getActivity(), params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    HomeDataBean homeDataBean = (HomeDataBean) MapToBeanUtil.toJavaBean(result.getParams(), HomeDataBean.class);
                    initView(homeDataBean);
                } else {
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                if (NetUtils.isConnected(getActivity())) {
                    Toast(getString(R.string.service_error));
                } else {
                    Toast(getString(R.string.network_fails));
                }
            }
        });
    }

    private void initView(HomeDataBean homeDataBean) {
        try {
            if (homeDataBean == null) return;
            textSalesCount.setText(homeDataBean.getSalesCount() + "");
            textAlreadyInstall.setText(homeDataBean.getAlreadyInstallCount() + "");
            textNoInstall.setText(homeDataBean.getNoInstallCount() + "");
            List<SalesRankingBean> salesRankingBeenList = homeDataBean.getSalesRanking();
            if (salesRankingBeenList != null) {
                if (salesRankingBeenList.size() > 0) {
                    String name = salesRankingBeenList.get(0).getUserName();
                    textSalesName1.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textSalesCount1.setText(salesRankingBeenList.get(0).getSalesCount() + "");
                }
                if (salesRankingBeenList.size() > 1) {
                    String name = salesRankingBeenList.get(1).getUserName();
                    textSalesName2.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textSalesCount2.setText(salesRankingBeenList.get(1).getSalesCount() + "");
                }
                if (salesRankingBeenList.size() > 2) {
                    String name = salesRankingBeenList.get(2).getUserName();
                    textSalesName3.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textSalesCount3.setText(salesRankingBeenList.get(2).getSalesCount() + "");
                }
            }
            List<InstallRankingBean> installRankingList = homeDataBean.getInstallRanking();
            if (installRankingList != null) {
                if (installRankingList.size() > 0) {
                    String name = installRankingList.get(0).getUserName();
                    textInstallName1.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textInstallCount1.setText(installRankingList.get(0).getInstallCount() + "");
                }
                if (installRankingList.size() > 1) {
                    String name = installRankingList.get(1).getUserName();
                    textInstallName2.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textInstallCount2.setText(installRankingList.get(1).getInstallCount() + "");
                }
                if (installRankingList.size() > 2) {
                    String name = installRankingList.get(2).getUserName();
                    textInstallName3.setText(name.length() > 6 ? name.substring(0, 5) + "..." : name);
                    textInstallCount3.setText(installRankingList.get(2).getInstallCount() + "");
                }
            }
        } catch (Exception e) {
        }
    }

    public void onEventMainThread(KeyDownEvent event) {
        if (event.getPosition() == 0) {
            if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
                ((MainActivity) getActivity()).exit();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
